﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Keepo.Models;
using System.ComponentModel.DataAnnotations;
using System.IO;
using AlbumDesigner.Helpers;
using AlbumDesigner.Models.Enums;

namespace AlbumDesigner.Models.Types
{
    public class AlbumData
    {
        public int AlbumId { get; set; }
        public string Title { get; set; }

        public int NumOfPages { get; set; }
        public int NumOfCopies { get; set; }

        public int OrderNum { get; set; }

        public int PaperId { get; set; }

        public string DueDate { get; set; }

        public int AlbumStatusId { get; set; }
        public string AlbumStatus { get; set; }
        public int AlbumLockStatusId { get; set; }
        public string AlbumLockStatus { get; set; }

        public int AlbumSizeId { get; set; }
        public string AlbumSize { get; set; }

        public string StudioNotes { get; set; }

        public string EditorPassword { get; set; }

        [RegularExpression(@"\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b", ErrorMessage = "דוא\"ל לא תקין")]
        public string EditorEmail { get; set; }
        public string EditorFirstName { get; set; }
        public string EditorLastName { get; set; }
        public string EditorPhone { get; set; }
        public string EditorMobile { get; set; }

        public int? SalesManId { get; set; }
        public string SalesMan { get; set; }

        public bool HasPhotographer { get; set; }
        public bool IncludeGraphics { get; set; }
        public int IntroPagesNumber { get; set; }

        public bool HasRender { get; set; }
        public bool IsRemoved { get; set; }
        public string CreatedDate { get; set; }
        public bool IsOneSided { get; set; }

        public int TotalPagesCount { get; set; }
        public int RenderedPagesCount { get; set; }

        public string ContractFile { get; set; }

        public int LastPrintVersion { get; set; }

        public string DirectorySize { get; set; }

        public string CoverThumbnail { get; set; }

        public AlbumData()
        {

        }

        public AlbumData(Album album, bool calcSize = false)
        {
            this.AlbumId = album.AlbumId;
            this.Title = album.Title;
            this.NumOfPages = album.NumOfPages;
            this.NumOfCopies = album.NumOfCopies;
            this.OrderNum = album.OrderNum;
            this.DueDate = album.DueDate.ToShortDateString();
            this.PaperId = album.PaperId;

            this.AlbumSizeId = album.AlbumSizeId;
            this.AlbumSize = "-";
            if (album.AlbumSize != null)
            {
                this.AlbumSize = album.AlbumSize.Title;
            }

            this.AlbumStatusId = album.AlbumStatusId;
            this.AlbumStatus = "-";
            if (album.AlbumStatus != null)
            {
                this.AlbumStatus = album.AlbumStatus.Title;
            }

            this.AlbumLockStatusId = album.AlbumLockStatusId;
            this.AlbumLockStatus = "-";
            if (album.AlbumLockStatus != null)
            {
                this.AlbumLockStatus = album.AlbumLockStatus.Title;
            }

            this.StudioNotes = album.StudioNotes;

            this.EditorFirstName = "-";
            this.EditorLastName = "-";
            this.EditorEmail = "-";
            this.EditorPassword = "-";
            this.EditorPhone = "-";
            this.EditorMobile = "-";

            if (album.Editor != null)
            {
                this.EditorFirstName = album.Editor.FirstName;
                this.EditorLastName = album.Editor.LastName;
                this.EditorEmail = album.Editor.Email;
                this.EditorPassword = album.Editor.Password;
                this.EditorPhone = album.Editor.Phone;
                this.EditorMobile = album.Editor.Mobile;
            }

            this.SalesManId = album.SalesManId;
            this.SalesMan = "-";
            if (album.SalesMan != null)
            {
                this.SalesMan = album.SalesMan.FirstName + " " + album.SalesMan.LastName;
            }

            this.HasPhotographer = album.HasPhotographer;
            this.IncludeGraphics = album.IncludeGraphics;
            this.IntroPagesNumber = album.IntroPagesNumber;

            this.IsRemoved = album.IsRemoved;
            this.HasRender = album.HasRender;
            this.CreatedDate = album.CreatedDate.ToShortDateString();
            this.IsOneSided = album.IsOneSided;

            //count the number of total pages in this album's sections
            this.TotalPagesCount = album.AlbumSections.Sum(m => m.AlbumPages.Count);

            //count the number of renedered pages in this album's sections
            var renderedPagesCount = 0;
            album.AlbumSections.ToList().ForEach(m => renderedPagesCount += m.AlbumPages.Where(n => n.HasRender).Count());
            this.RenderedPagesCount = renderedPagesCount;

            //calculate the last print version
            this.LastPrintVersion = 1;
            var lastVersion = album.AlbumPrintVersions.OrderByDescending(m => m.VersionIdx).FirstOrDefault();
            if (lastVersion != null)
            {
                this.LastPrintVersion = lastVersion.VersionIdx;
            }

            var albumDir = HttpContext.Current.Server.MapPath("~/resources/albums/" + album.AlbumId);
            this.ContractFile = "";
            this.DirectorySize = "-";

            //calculate the album directory's size
            if (Directory.Exists(albumDir))
            {
                var contractDir = albumDir + "/contract";

                if (Directory.Exists(contractDir))
                {
                    //search contract files with this album id
                    var files = Directory.GetFiles(contractDir);

                    if (files.Length > 0)
                    {
                        this.ContractFile = "~/resources/albums/" + album.AlbumId + "/contract/" + Path.GetFileName(files[0]);
                    }
                }

                if (calcSize)
                {
                    var directorySize = FilesHelper.GetDirectorySize(albumDir);
                    this.DirectorySize = FilesHelper.BytesToMegaBytes(directorySize);
                }
                else
                {
                    this.DirectorySize = "not calculated";
                }
            }

            //get the url of the Thumbnail of the album's Cover
            var coverSection = album.AlbumSections.FirstOrDefault(m => m.AlbumSectionTypeId == (int)AlbumSectionTypeEnum.Cover);
            if (coverSection != null)
            {
                var page = coverSection.AlbumPages.FirstOrDefault();
                if (page.HasThumbnail)
                {
                    CoverThumbnail = AlbumHelper.PageThumbnailUrl(album.AlbumId, page.AlbumPageId);
                }
            }
        }
    }
}